package com.zj.sy.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.zj.sy.mapper.CommMapper;
import com.zj.sy.service.CommService;
import com.zj.sy.utils.StringUtil;
import org.checkerframework.checker.units.qual.A;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.sql.Array;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @description:
 * @author: Carver Zhang
 * @create: 2021-08-11 21:29
 **/
@Service
public class CommServiceImpl implements CommService {

    @Resource
    private CommMapper commMapper;

    @Override
    public Map<String, Integer> getYBSS(String pos) {
        Map<String, Integer> result = new HashMap<>();
        result.put("bzdz", this.commMapper.countsByBzdz(pos));
        result.put("syfw", this.commMapper.countsBySyfw(pos));
        result.put("syrk", this.commMapper.countsBySyrk(pos));
        Integer sydwCount = this.commMapper.countsByFgyzjjzzb(pos)
                + this.commMapper.countsByShzzb(pos)
                + this.commMapper.countsByXxb(pos)
                + this.commMapper.countsByZjtdb(pos)
                + this.commMapper.countsByJmxqb(pos)
                + this.commMapper.countsByJdwldb(pos)
                + this.commMapper.countsByZddqb(pos);
        result.put("sydw", sydwCount);
        result.put("csbj", this.commMapper.countsByCsbj(pos));
        return result;
    }

    @Override
    public BigDecimal getCityArea(String pos) {
        return this.commMapper.getCityArea(pos);
    }

    @Override
    public Map<Object, Object> getGridTeam(String pos) {
        Map<Object, Object> r = new HashMap<>();
        List<Map<String, Object>> result = this.commMapper.getGridTeam(pos);
        result.forEach(m -> r.put(m.get("auth"), m.get("count")));

        //网格员数量
        Integer wgyNum = this.commMapper.getWgyNum(pos);
        r.put(1, wgyNum);
        return r;
    }

    @Override
    public Map<Object, Object> getGridNum(String pos) {
        Map<Object, Object> r = new HashMap<>();
        List<Map<String, Object>> result = this.commMapper.getGridNum2();
        Map<String, Object> stringObjectMap = result.get(0);
        String wgsl_data_sl = (String)stringObjectMap.get("wgsl_data_sl");
        List<String> s = Arrays.asList(wgsl_data_sl.split(","));
        r.put("cswg", s.get(0));
        r.put("zswg", s.get(1));
        r.put("ncwg", s.get(2));
        r.put("wwg", s.get(3));
        //全要素
        String qys = (String)stringObjectMap.get("qys");
        List<String> s2 = Arrays.asList(qys.split(","));
        r.put("bzdz", s2.get(0));
        r.put("syfw", s2.get(1));
        r.put("syrk", s2.get(2));
        r.put("sydw", s2.get(3));
        r.put("csbj", s2.get(4));

        String wgdl_data_fwl = (String)stringObjectMap.get("wgdl_data_fwl");
        List<String> s3 = Arrays.asList(wgdl_data_fwl.split(","));
        r.put("h1", s3.get(0));
        r.put("h2", s3.get(1));
        r.put("h3", s3.get(2));
        r.put("h4", s3.get(3));

        String wgdw = (String)stringObjectMap.get("wgdw");
        List<String> s0 = Arrays.asList(wgdw.split(","));
        r.put("wgy", s0.get(0));
        r.put("sgyl", s0.get(1));

        String csmj = (String)stringObjectMap.get("csmj");
        r.put("csmj", csmj);

        String wgdl_data_jqr_7rq_percent = (String)stringObjectMap.get("wgdl_data_jqr_7rq_percent");
        List<String> s4 = Arrays.asList(wgdl_data_jqr_7rq_percent.split(","));
        String p1 = s4.get(0);
        r.put("h5", p1.charAt(0));
        r.put("h6", p1.charAt(1));
        r.put("h7", p1.charAt(3));
        r.put("h8", p1.charAt(4));
        String p2 = s4.get(1);
        r.put("h9", p2.charAt(0));
        r.put("h10", p2.charAt(1));
        r.put("h11", p2.charAt(3));
        r.put("h12", p2.charAt(4));

        List<List<Integer>> ss = new ArrayList<>();
        String wgdl_data_zr = (String)stringObjectMap.get("wgdl_data_zr");
        List<Integer> s6 = (Arrays.asList(wgdl_data_zr.split(","))).stream().mapToInt(Integer::parseInt).boxed().collect(Collectors.toList());

        ss.add(s6);
        String wgdl_data_jr = (String)stringObjectMap.get("wgdl_data_jr");
        List<Integer> s7 = Arrays.asList(wgdl_data_jr.split(",")).stream().mapToInt(Integer::parseInt).boxed().collect(Collectors.toList());
        ss.add(s7);
        String wgdl_data_7rq = (String)stringObjectMap.get("wgdl_data_7rq");
        List<Integer> s8 = Arrays.asList(wgdl_data_7rq.split(",")).stream().mapToInt(Integer::parseInt).boxed().collect(Collectors.toList());
        ss.add(s8);
        r.put("chartData", ss);
        return r;
    }

    @Override
    public List<Map<String, Object>> getGriderRank(String pos, String year, String month, int sort) {
        if (StringUtil.isEmpty(year)) {
            year = String.valueOf(DateUtil.year(new Date()));
        }
        if (StringUtil.isEmpty(month)) {
            month = String.valueOf(DateUtil.month(new Date()));
        }
        String time = year + "-" + month;
        String startTime = DateUtil.formatDate(DateUtil.beginOfMonth(DateUtil.parse(time, "yyyy-MM")));
        String endTime = DateUtil.formatDate(DateUtil.endOfMonth(DateUtil.parse(time, "yyyy-MM")));
        List<String> pics = this.commMapper.getPics(sort == 1 ? "ASC" : "DESC");
        List<Map<String, Object>> result = this.commMapper.getGriderRank(pos, startTime, endTime, sort == 1 ? "ASC" : "DESC");
        for (int i = 0; i < result.size(); i++) {
            Map<String, Object> map = result.get(i);
            map.put("headPic", pics.get(i));
        }
        return result;
    }

    @Override
    public Map<String, Integer> getDealEventNums(String pos) {
        int lastMonth = calcuteDealNums(pos, DateUtil.beginOfMonth(DateUtil.lastMonth()), DateUtil.endOfMonth(DateUtil.lastMonth()),1);
        int thisMonth = calcuteDealNums(pos, DateUtil.beginOfMonth(DateUtil.date()), DateUtil.endOfMonth(DateUtil.date()),1);
        int thisYear = calcuteDealNums(pos, DateUtil.beginOfYear(DateUtil.date()), DateUtil.endOfYear(DateUtil.date()),1);
        int lastDay = calcuteDealNums(pos, DateUtil.beginOfDay(DateUtil.yesterday()), DateUtil.endOfDay(DateUtil.yesterday()),1);
        int today = calcuteDealNums(pos, DateUtil.beginOfDay(DateUtil.date()), DateUtil.endOfDay(DateUtil.date()),1);
        Map<String, Integer> result = new HashMap<>();
        result.put("lastMonth", lastMonth);
        result.put("thisMonth", thisMonth);
        result.put("thisYear", thisYear);
        result.put("lastDay", lastDay);
        result.put("today", today);
        return result;
    }

    @Override
    public List<Map<String, Object>> getRecentEvent(String pos) {
        List<Map<String, Object>> result = new ArrayList<>();
        result.addAll(this.commMapper.getEventByCsgl(pos));
        result.addAll(this.commMapper.getEventByShwd(pos));
        result.addAll(this.commMapper.getEventByGgaq(pos));
        return result.stream().sorted(Comparator.comparing(CommServiceImpl::comparingByTime).reversed())
                .collect(Collectors.toList());
    }

    @Override
    public List<Map<String, Object>> getRecentEvent2(String pos, String type, String wgbm, String state) {
        List<Map<String, Object>> result = new ArrayList<>();
        result.addAll(this.commMapper.getRecentEvent2(pos, type, wgbm, state));
        return result.stream().sorted(Comparator.comparing(CommServiceImpl::comparingByTime).reversed())
                .collect(Collectors.toList());
    }

    @Override
    public List<Map<String, Object>> getRecentEvent3(String pos, String type, String wgbm, String state) {
        List<Map<String, Object>> result = new ArrayList<>();
        result.addAll(this.commMapper.getRecentEvent3(pos, type, wgbm, state));
        return result.stream().collect(Collectors.toList());
    }

    @Override
    public List<Map<String, Object>> getOneToFiveEvent(String pos, String wgbm) {
    	return this.commMapper.getOneToFiveEvent(pos, wgbm);
    }

    @Override
    public List<Map<String, Object>> getOneToFiveEventDo(String busiNumber) {
    	return this.commMapper.getOneToFiveEventDo(busiNumber);
    }

    @Override
    public Map<String, Integer> getThisMonthEventCounts(String pos) {
        Map<String, Integer> result = new HashMap<>();
        result.put("shza", this.commMapper.countsShzaThisMonth(pos, DateUtil.format(DateUtil.beginOfMonth(DateUtil.date()), "yyyy-MM-dd HH:mm:ss"),
                DateUtil.format(DateUtil.endOfMonth(DateUtil.date()), "yyyy-MM-dd HH:mm:ss")));
        result.put("mdjf", this.commMapper.countsMdjfThisMonth(pos, DateUtil.format(DateUtil.beginOfMonth(DateUtil.date()), "yyyy-MM-dd HH:mm:ss"),
                DateUtil.format(DateUtil.endOfMonth(DateUtil.date()), "yyyy-MM-dd HH:mm:ss")));
        result.put("ggaq", this.commMapper.countsGgaqThisMonth(pos, DateUtil.format(DateUtil.beginOfMonth(DateUtil.date()), "yyyy-MM-dd HH:mm:ss"),
                DateUtil.format(DateUtil.endOfMonth(DateUtil.date()), "yyyy-MM-dd HH:mm:ss")));
        result.put("csgl", this.commMapper.countsCsglThisMonth(pos, DateUtil.format(DateUtil.beginOfMonth(DateUtil.date()), "yyyy-MM-dd HH:mm:ss"),
                DateUtil.format(DateUtil.endOfMonth(DateUtil.date()), "yyyy-MM-dd HH:mm:ss")));
        result.put("hjbh", this.commMapper.countsHjbhThisMonth(pos, DateUtil.format(DateUtil.beginOfMonth(DateUtil.date()), "yyyy-MM-dd HH:mm:ss"),
                DateUtil.format(DateUtil.endOfMonth(DateUtil.date()), "yyyy-MM-dd HH:mm:ss")));
        result.put("msfw", this.commMapper.countsMsfwThisMonth(pos, DateUtil.format(DateUtil.beginOfMonth(DateUtil.date()), "yyyy-MM-dd HH:mm:ss"),
                DateUtil.format(DateUtil.endOfMonth(DateUtil.date()), "yyyy-MM-dd HH:mm:ss")));
        result.put("shwd", this.commMapper.countsShwdThisMonth(pos, DateUtil.format(DateUtil.beginOfMonth(DateUtil.date()), "yyyy-MM-dd HH:mm:ss"),
                DateUtil.format(DateUtil.endOfMonth(DateUtil.date()), "yyyy-MM-dd HH:mm:ss")));
        return result;
    }

    @Override
    public Map<String, List<Map<String, Object>>> getThisYearEventCounts(String pos) {
        Map<String, List<Map<String, Object>>> result = new HashMap<>();
        result.put("shza", this.commMapper.countsShzaYear(pos));
        result.put("mdjf", this.commMapper.countsMdjfYear(pos));
        result.put("ggaq", this.commMapper.countsGgaqYear(pos));
        result.put("csgl", this.commMapper.countsCsglYear(pos));
        result.put("hjbh", this.commMapper.countsHjbhYear(pos));
        result.put("msfw", this.commMapper.countsMsfwYear(pos));
        result.put("shwd", this.commMapper.countsShwdYear(pos));
        return result;
    }

    @Override
    public Map<String, Object> getGridCounts(String pos) {
        Map<String, Object> r = new HashMap<>();
        List<Map<String, Object>> result = this.commMapper.countsGrid(pos);
        result.forEach(m -> {
            r.put(m.get("xzqhdm").toString(), m.get("count"));
        });
        return r;
    }

    @Override
    public Map<String, Object> getAllPopulation(String pos) {
        Map<String, Object> r = new HashMap<>();
        List<Map<String, Object>> result = this.commMapper.getAllPopulation(pos);
        result.forEach(m -> {
            // @人口类型
            // 10=liudong 流动人口
            // 20=changzhu 常驻人口
            // 30=jingwai 境外人口
            r.put(m.get("sz_syrk_type").toString(), m.get("count"));
        });
        return r;
    }

    @Override
    public Map<String, Object> getPermanentPopulation(String pos) {
        Map<String, Object> r = new HashMap<>();
        List<Map<String, Object>> result = this.commMapper.getPermanentPopulation(pos);
        result.forEach(m -> {
            r.put((String) m.get("xzqhdm"), m.get("coalesce"));
        });
        return r;
    }

    @Override
    public List<Map<String, List<List<Map<String, Object>>>>> getFloatingPopulation(String pos) {
        List<Map<String, List<List<Map<String, Object>>>>> r = new ArrayList<>();
        List<Map<String, Object>> floatingPopulation = this.commMapper.getFloatingPopulation(pos);
        List<List<Map<String, Object>>> list1 = new ArrayList<>();
        Map<String, Object> zj = new HashMap<>();
        zj.put("name", "镇江");
        floatingPopulation.forEach(stringObjectMap -> {
            List<Map<String, Object>> list2 = new ArrayList<>();
            list2.add(zj);
            list2.add(stringObjectMap);
            list1.add(list2);
        });
        Map<String, List<List<Map<String, Object>>>> map = new HashMap<>();
        map.put("SWData", list1);
        r.add(map);
        return r;
    }

    @Override
    public List<Map<String, Object>> getHomicideCase(String pos, Integer year) {
        return this.commMapper.getHomicideCase(pos, year);
    }

    @Override
    public Map<String, Object> getGridTeam2(String pos) {
        Map<String, Object> r = new HashMap<>();
        List<Map<String, Object>> sgyl = this.commMapper.getCountSGYL(pos);
        sgyl.forEach(m -> {
            r.put(String.valueOf(m.get("id")), m.get("count"));
        });
//        List<Map<String, Object>> wgy = this.commMapper.getCountWGY(pos);
//        wgy.forEach(m -> {
//            r.put("wgy", m.get("count"));
//        });

        Integer wgyNum = this.commMapper.getWgyNum(pos);
        r.put("wgy", wgyNum);

        List<Map<String, Object>> njy = this.commMapper.getCountNJY(pos);
        njy.forEach(m -> {
            r.put("njy", m.get("count"));
        });
        return r;
    }

    @Override
    public Map<String, Map<String, Object>> getDisputeNums(String pos) {
        Map<String, Map<String, Object>> r = new HashMap<>();
        List<Map<String, Object>> disputeNums = this.commMapper.getDisputeNums(pos);
        disputeNums.forEach(m -> {
            String date = m.get("date").toString();
            String hjsfcg_pdbz = (String) m.get("hjsfcg_pdbz");
            Map<String, Object> map = r.get(date) != null ? r.get(date) : new HashMap<>();
            map.put(hjsfcg_pdbz, m.get("count"));
            r.put(date, map);
        });
        return r;
    }

    @Override
    public List<Map<String, Object>> getZMWGY(String pos) {
        return this.commMapper.getZMWGY(pos);
    }

    @Override
    public Map<String, LinkedHashMap<String, Object>> getPointsRanking(String pos) {
        Map<String, LinkedHashMap<String, Object>> r = new HashMap<>();
        LinkedHashMap<String, LinkedHashMap<String, Object>> rr = new LinkedHashMap<>();
        List<Map<String, Object>> result = this.commMapper.getPointsRanking(pos);
        result.forEach(m -> {
            LinkedHashMap<String, Object> rm;
            String xzqhdm = (String) m.get("xzqhdm");
            String xm = (String) m.get("xm");
            rm = r.get(xzqhdm) != null ? r.get(xzqhdm) : new LinkedHashMap<>();
            if (StringUtil.isNotEmpty(xm)) {
                rm.put(xm, m.get("sum"));
            }
            r.put(xzqhdm, rm);
        });

        rr.put("321181", r.get("321181"));
        rr.put("321183", r.get("321183"));
        rr.put("321182", r.get("321182"));
        rr.put("321112", r.get("321112"));

        rr.put("321102", r.get("321102"));
        rr.put("321111", r.get("321111"));
        rr.put("321192", r.get("321192"));
        rr.put("321193", r.get("321193"));

        return rr;
    }

    @Override
    public Map<String, Map<String, Object>> getFocus(String pos) {
        Map<String, Map<String, Object>> r = new HashMap<>();
        List<Map<String, Object>> zdry = this.commMapper.getZDRY(pos);
        Map<String, Object> zdryMap = new HashMap<>();
        zdry.forEach(m -> {
            zdryMap.put((String) m.get("name"), m.get("count"));
        });
        List<Map<String, Object>> tsry = this.commMapper.getTSRY(pos);
        Map<String, Object> tsryMap = new HashMap<>();
        tsry.forEach(m -> {
            tsryMap.put((String) m.get("name"), m.get("count"));
        });
        List<Map<String, Object>> ghdx = this.commMapper.getGHDX(pos);
        Map<String, Object> ghdxMap = new HashMap<>();
        ghdx.forEach(m -> {
            ghdxMap.put((String) m.get("name"), m.get("count"));
        });
        r.put("zdry", zdryMap);
        r.put("tsry", tsryMap);
        r.put("ghdx", ghdxMap);
        return r;
    }

    @Override
    public Map<String, Map<Object, Map<String, Object>>> getAssessmentRankings(String pos) {
        Map<String, Map<Object, Map<String, Object>>> r = new HashMap<>();
        List<Map<String, Object>> rankings = this.commMapper.getAssessmentRankings(pos);
        rankings.forEach(m -> {
            String xzqhdm = (String) m.get("xzqhdm");
            Map<Object, Map<String, Object>> rm = r.get(xzqhdm);
            if (rm == null) {
                rm = new HashMap<>();
            }
            Map<String, Object> map = new HashMap<>();
            map.put((String) m.get("xzqhmc"), m.get("score"));
            rm.put(m.get("sort"), map);
            r.put(xzqhdm, rm);
        });
        return r;
    }

    @Override
    public List<Map<String, Object>> getBuilding(String pos) {
        return this.commMapper.selectBuilding(pos);
    }

    @Override
    public List<Map<String, Object>> getUnit(String pos) {
        return this.commMapper.selectUnit(pos);
    }

    @Override
    public List<Map<String, Object>> getTownsRank(String pos, Boolean desc, Integer year, Integer month, Integer limit) {
        return this.commMapper.selectTownsRank(pos, desc, year, month, limit);
    }

    private static Date comparingByTime(Map<String, Object> map) {
        return (Date) map.get("time");
    }

    private int calcuteDealNums(String pos, DateTime startTime, DateTime endTime,int type) {
        if(type ==1){
            String format = DateUtil.format(startTime, "yyyy-MM-dd HH:mm:ss");
            String format1 = DateUtil.format(endTime, "yyyy-MM-dd HH:mm:ss");
            return this.commMapper.countsByShwd2(pos,format, format1);
        }else if(type ==2){
            return this.commMapper.countsByNetInfo(pos, DateUtil.format(startTime, "yyyy-MM-dd HH:mm:ss"), DateUtil.format(endTime, "yyyy-MM-dd HH:mm:ss"));
        }else if(type ==3){
            return this.commMapper.countsByMdpc(pos, DateUtil.format(startTime, "yyyy-MM-dd HH:mm:ss"), DateUtil.format(endTime, "yyyy-MM-dd HH:mm:ss"));
        }else if(type ==4){
            return this.commMapper.countsByGgaq(pos, DateUtil.format(startTime, "yyyy-MM-dd HH:mm:ss"), DateUtil.format(endTime, "yyyy-MM-dd HH:mm:ss"));
        }else if(type ==5){
            return this.commMapper.countsByMyss(pos, DateUtil.format(startTime, "yyyy-MM-dd HH:mm:ss"), DateUtil.format(endTime, "yyyy-MM-dd HH:mm:ss"));
        }else if(type ==6){
            //  return  this.commMapper.countsByZcfg(pos, DateUtil.format(startTime, "yyyy-MM-dd HH:mm:ss"), DateUtil.format(endTime, "yyyy-MM-dd HH:mm:ss"))
        }else if(type ==7){
            return this.commMapper.countsByCsgl(pos, DateUtil.format(startTime, "yyyy-MM-dd HH:mm:ss"), DateUtil.format(endTime, "yyyy-MM-dd HH:mm:ss"));
        }
        return 0;
    }



}


